﻿/*
 * @author: wizardc
 */

using System.Collections.Generic;
using UnityEngine;

namespace DouEditor.Extend
{
    /// <summary>
    /// Editor非运行模式下快捷查看特效预制（支持Animator与ParticleSystem）的工具组件，支持速率调整
    /// </summary>
    [DisallowMultipleComponent]
    [ExecuteInEditMode]
    public class MixedQuickViewer : MonoBehaviour
    {
        public List<Animator> animatorList;
        public List<ParticleSystem> particleList;

        private MixedEditorPlayData _mixedEditorPlayData;

        [SerializeField]
        private int _frameRate = 30;
        private float _frameTime = 1f / 30;

        private MixedEditorPlayInfo _mixedEditorPlayInfo;

        private MixedEditorPlayInfo MixedEditorPlayInfo
        {
            get
            {
                if (_mixedEditorPlayInfo == null)
                {
                    CollectEffectComponent();
                    _mixedEditorPlayInfo = MixedEditorPlayMgr.instance.AddData(_mixedEditorPlayData);
                }
                return _mixedEditorPlayInfo;
            }
        }

        public bool playing
        {
            get
            {
                if (MixedEditorPlayInfo != null)
                {
                    return MixedEditorPlayInfo.playing;
                }
                return false;
            }
        }

        public float currentTime
        {
            set
            {
                if (MixedEditorPlayInfo != null)
                {
                    MixedEditorPlayInfo.currentTime = value;
                }
            }
            get
            {
                if (MixedEditorPlayInfo != null)
                {
                    return MixedEditorPlayInfo.currentTime;
                }
                return 0;
            }
        }

        public float totalTime
        {
            get
            {
                if (MixedEditorPlayInfo != null)
                {
                    return MixedEditorPlayInfo.totalTime;
                }
                return 0;
            }
        }

        public int frameRate
        {
            set
            {
                _frameRate = value;
                _frameTime = 1f / _frameRate;
            }
            get => _frameRate;
        }

        public int currentFrame
        {
            set
            {
                if (totalTime > 0)
                {
                    currentTime = ((float) value / (float) totalFrame) * totalTime;
                }
            }
            get
            {
                if (totalTime == 0)
                {
                    return 0;
                }
                return Mathf.FloorToInt((float) currentTime / (float) totalTime * totalFrame);
            }
        }

        public int totalFrame
        {
            get
            {
                if (totalTime == 0)
                {
                    return 0;
                }
                return Mathf.CeilToInt((float) totalTime / _frameTime);
            }
        }

        public bool isLoop
        {
            set
            {
                if (MixedEditorPlayInfo != null)
                {
                    MixedEditorPlayInfo.isLoop = value;
                }
            }
            get
            {
                if (MixedEditorPlayInfo != null)
                {
                    return MixedEditorPlayInfo.isLoop;
                }
                return false;
            }
        }

        public float ratio
        {
            set
            {
                if (MixedEditorPlayInfo != null)
                {
                    MixedEditorPlayInfo.ratio = value;
                }
            }
            get
            {
                if (MixedEditorPlayInfo != null)
                {
                    return MixedEditorPlayInfo.ratio;
                }
                return 1;
            }
        }

        private void CollectEffectComponent()
        {
            var animatorArr = GetComponentsInChildren<Animator>(false);
            animatorList = new List<Animator>(animatorArr);
            var particleSystemArr = GetComponentsInChildren<ParticleSystem>(false);
            particleList = new List<ParticleSystem>(particleSystemArr);
            _mixedEditorPlayData = MixedEditorPlayData.Collect(gameObject);
        }

        public void Play()
        {
            MixedEditorPlayInfo.Play();
        }

        public void Pause()
        {
            MixedEditorPlayInfo.Pause();
        }

        public void Resume()
        {
            MixedEditorPlayInfo.Resume();
        }

        public void Stop()
        {
            MixedEditorPlayInfo.Stop();
        }

        private void OnDestroy()
        {
            MixedEditorPlayMgr.instance.RemoveData(_mixedEditorPlayData);
        }
    }
}
